import axios from 'axios'
import { Message, Loading } from 'element-ui'; //项目已经全局引入element的话可以不单独引入
import store from '@/store'
import { http } from "@/utils/server"
import router from '@/router'
// const config2 = require('../../vue.config.js')

// let loading        //定义loading变量

// function startLoading() {    //使用Element loading-start 方法
//     loading = Loading.service({
//         lock: true,
//         text: '加载中……',
//         background: 'rgba(255,255,255, 0)'
//     })
// }

// function endLoading() {    //使用Element loading-close 方法
//     loading.close()
// }

//那么 showFullScreenLoading() tryHideFullScreenLoading() 要干的事儿就是将同一时刻的请求合并。
//声明一个变量 needLoadingRequestCount，每次调用showFullScreenLoading方法 needLoadingRequestCount + 1。
//调用tryHideFullScreenLoading()方法，needLoadingRequestCount - 1。needLoadingRequestCount为 0 时，结束 loading。

let needLoadingRequestCount = 0
export function showFullScreenLoading() {
    // if (needLoadingRequestCount === 0) {
    //     startLoading()
    // }
    // needLoadingRequestCount++
}

export function tryHideFullScreenLoading() {
    //     if (needLoadingRequestCount <= 0) return
    //     needLoadingRequestCount--
    //     if (needLoadingRequestCount === 0) {
    //         endLoading()
    //     }
}

// create an axios instance
const service = axios.create({
    baseURL: process.env.VUE_APP_BASE_API, // url = base url + request url
    // baseURL:'http://his.boxinyao.com', // url = base url + request url

    // withCredentials: true, // send cookies when cross-domain requests
    timeout: 0, // request timeout
    crossDomain: true,
})
    // 添加请求拦截
service.interceptors.request.use(
        config => {
            if (localStorage.getItem("token")) {
                config.headers["Authorization"] = "Bearer " + localStorage.getItem("token")


            }
            if (config.params) {
                config.data = config.params
            }
            if (config.upData) {
                config.headers['Content-Type'] = 'multipart/form-data'
            } else {
                if (config.method === "post" || config.method === "POST") {
                    config.headers['Content-Type'] = 'application/json'
                    config.params = ""
                }
            }
            // console.log(config.data,"值a")
            // if (config.is_loading != 1) {
            //     showFullScreenLoading()
            // }

            return config
        },
        error => {
            return Promise.reject(error)
        }
    )
    // 添加响应拦截
service.interceptors.response.use(
    response => {


        tryHideFullScreenLoading()
            // console.log(response,"进入")
            // console.log(response.data, "code")
            // if (response.code == 200) {
            //     return response.data
            // } else if (response.code != 200) {
            //     let arr = {
            //         message: response.msg,
            //         code: response.code,
            //         data: response.data
            //     }
            //     Message({
            //         message: response.msg,
            //         type: 'error',
            //         duration: 2000
            //     })

        //     return arr
        // } else
        if (response.data) {
            if (response.data.code === 200) {
                return response.data
            } else if (response.data.code == -1) {
                console.log("response", response);


                let arr = {
                    message: response.data.msg,
                    code: response.data.code
                }
                if (response.request.responseURL.indexOf('/clinic_doctor/getMedicalRecordDetail') != -1) {
                    return arr;
                }
                Message({
                    message: response.data.message ? response.data.message : response.data.msg,
                    type: 'error',
                    duration: 2000
                })
                return arr
            } else {
                let arr = {
                    message: response.data.message,
                    code: response.data.code,
                    data: response.data
                }
                Message({
                    message: response.data.message ? response.data.message : response.data.msg,
                    type: 'error',
                    duration: 2000
                })

                return arr
            }
        } else {
            console.log(response)
            if (response.code == 200) {
                return response.data
            } else if (response.code != 200) {
                let arr = {
                    message: response.message,
                    code: response.code,
                    data: response.data
                }
                Message({
                    message: response.message,
                    type: 'error',
                    duration: 2000
                })

                return arr
            }
        }



    },
    error => {
        console.log(error)
        if (error.response.status == 401) {

            localStorage.removeItem("token")
                // Message({
                //     message: "",
                //     type: 'error',
                //     duration: 2000
                // })
            localStorage.clear();
            router.replace({ //跳转到登录页面
                path: '/login',
                // 将跳转的路由path作为参数，登录成功后跳转到该路由
                query: { redirect: router.currentRoute.fullPath }
            })
        }
        let err = {
            status: error.response.status,
            message: error.response.data.message
        }
        Message({
            message: error.response.data.message,
            type: 'error',
            duration: 2000
        })
        return Promise.reject(err)
    }
)

export default service
